import {mSelectDevTypeByPage, selectDevTypeData} from '@/api/device'
import useDevTypeConConfig from './conConfig'
import useDevTypeBaseDataConfig from './baseDataConfig'
import {message} from '@/libs/global/antDesignVue'
import deepClone from '@/libs/deepClone'
import useDevTypeFunConfig from './funConfig'

function useDeviceType() {
  const useConConfig = useDevTypeConConfig()
  const {devTypeConConfig2devTypeModuleConConfig, initDevTypeConConfig} = useConConfig

  const useBaseDataConfig = useDevTypeBaseDataConfig()
  const {devTypeBaseDataConfig2devTypeModuleBaseDataConfig, initDevTypeBaseDataConfig} =
    useBaseDataConfig

  const useFunConfig = useDevTypeFunConfig()
  const {devTypeFunConfig2devTypeModuleFunConfig, initDevTypeFunConfig} = useFunConfig

  const defaultDeviceTypeConfig: DeviceType.DevTypeConfig = {
    devTypeId: -1,
    devTypeName: '',
    devTypeDesc: '',
    devTypeBaseDataList: [],
    devTypeFunList: [],
    devTypeCollectRate: {
      devTypeCrId: -1,
      devTypeId: -1,
      devTypeCRCode: '0009',
      devTypeCRValue: ''
    },
    devTypeSwitch: {
      devTypeSwitchId: -1,
      devTypeId: -1,
      devTypeSwitchCode: '0064',
      devTypeSwitchValue: '0'
    },
    devTypeConConfig: {
      devTypeCCId: -1,
      devTypeTxTypeCode: '0017',
      devTypeDataTypeCode: '0019',
      devTypeHeart: '',
      devTypeId: -1,
      paramConfList: []
    }
  }
  let deviceTypeConfig = ref<DeviceType.DevTypeConfig>(deepClone(defaultDeviceTypeConfig))

  function setDeviceTypeConfig(data: DeviceType.DevTypeConfig) {
    deviceTypeConfig.value = data
  }

  const getDeviceTypeConfig = computed(() => deviceTypeConfig.value)

  function updateDeviceTypeConfig(option?: {
    devTypeId?: number
    success?: (data: DeviceType.DevTypeConfig) => void
    error?: Function
  }) {
    if (option && option.devTypeId) {
      selectDevTypeData({devTypeId: option.devTypeId})
        .then(res => {
          setDeviceTypeConfig(res.data)

          devTypeBaseDataConfig2devTypeModuleBaseDataConfig(res.data.devTypeBaseDataList)

          devTypeFunConfig2devTypeModuleFunConfig(res.data.devTypeFunList)

          devTypeConConfig2devTypeModuleConConfig(res.data.devTypeConConfig)

          option.success?.(res.data)
          message.success('查询成功')
        })
        .catch(err => {
          setDeviceTypeConfig(deepClone(defaultDeviceTypeConfig))
          initDevTypeConConfig()
          initDevTypeBaseDataConfig()
          initDevTypeFunConfig()
          option.error?.()
          message.error('查询失败')
        })
    } else {
      setDeviceTypeConfig(deepClone(defaultDeviceTypeConfig))
    }
  }

  let simpleDevTypeList = ref<DeviceType.SimpleType[]>([])
  function setSimpleDevTypeList(data: DeviceType.SimpleType[]) {
    simpleDevTypeList.value = data
  }
  /**获取所有设备类型 */
  function updateSimpleDeviceTypeList(option?: {
    devTypeName?: string
    success?: (data: DeviceType.SimpleType[]) => void
    error?: Function
  }) {
    mSelectDevTypeByPage({
      page: 1,
      size: 99999,
      devTypeName: option?.devTypeName || ''
    })
      .then(res => {
        setSimpleDevTypeList(res.data.list)
        option && option.success && option.success(res.data.list)
      })
      .catch(err => {
        setSimpleDevTypeList([])
        option && option.error && option.error()
      })
  }

  return {
    ...useConConfig,

    ...useBaseDataConfig,

    ...useFunConfig,

    deviceTypeConfig: getDeviceTypeConfig,

    updateDeviceTypeConfig,
    simpleDevTypeList,
    updateSimpleDeviceTypeList
  }
}
export default useDeviceType
